package com.example.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("manager") DefaultWebSecurityManager manager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();


        /*
        * authc:必须认证才能访问
        * user:必须拥有记住我的功能才能访问
        *perms:必须拥有对某个资源的权限才能访问
        *role:拥有某个角色权限才能访问
        * */
        LinkedHashMap<String, String> hashMap = new LinkedHashMap<>();


        //需要授权的页面
        hashMap.put("/html/admin","perms[admin]");
        //设置需要授权登录的页面
        hashMap.put("/insertRR","authc");
        hashMap.put("/html/**","authc");
      /*  hashMap.put("/add","perms[add]");*/



/*

        //设置没有授权的用户跳转的·页面-顺序问题
        bean.setUnauthorizedUrl("/shou");
*/



        bean.setFilterChainDefinitionMap(hashMap);

        bean.setSecurityManager(manager);

        //设置没有认证的用户跳转的登录页面
        bean.setLoginUrl("/login");


        return bean;
    }

    @Bean
    public DefaultWebSecurityManager manager(@Qualifier("userRealm") UserRealm userRealm){

        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(userRealm);
        return defaultWebSecurityManager;
    }

    @Bean
    public UserRealm userRealm(){
        return new UserRealm();
    }

    @Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }
}
